/* 

This do file takes inputs of 

1. type1 (type of purchases transactions)
2. type2 (type of sales transactions)
3. begin (beginning of the sample period)
4. endd (end of the sample period)

and creates Table 3

*/

args type1 type2 begin endd

use "${root}\Data\corp_trade_data", clear


* Relevant transaction
gen buy = 0
bysort permno (date): replace buy = 1 if `type1'[_n+1] == 1 | `type1'[_n+2] == 1 | `type1'[_n+3] == 1 | `type1'[_n+4] == 1 | `type1'[_n+5] == 1 | `type1'[_n+6] == 1 
bysort permno (date): replace buy = 0 if `type2'[_n+1] == 1 | `type2'[_n+2] == 1 | `type2'[_n+3] == 1 | `type2'[_n+4] == 1 | `type2'[_n+5] == 1 | `type2'[_n+6] == 1

gen sal = 0
bysort permno (date): replace sal = 1 if `type2'[_n+1] == 1 | `type2'[_n+2] == 1 | `type2'[_n+3] == 1 | `type2'[_n+4] == 1 | `type2'[_n+5] == 1 | `type2'[_n+6] == 1
bysort permno (date): replace sal = 0 if `type1'[_n+1] == 1 | `type1'[_n+2] == 1 | `type1'[_n+3] == 1 | `type1'[_n+4] == 1 | `type1'[_n+5] == 1 | `type1'[_n+6] == 1

* Buy-minus-sell
keep if buy == 1 | sal == 1

* Quintiles
astile q_size = lagged_mktcap, by(date) nq(5)
drop if q_size == .
astile q_composite = composite, by(date q_size) nq(5)
drop if q_composite == .

* Sample period
keep if year >= `begin' & year <= `endd'

* EW
egen ret_e = mean(ret), by(q_composite buy date)

* VW
forvalues i = 1/5{
	forvalues j = 0/1{
		bysort date: asgen temp_ret_`i'_`j' = (ret/(q_composite == `i' & buy == `j')), w(lagged_mktcap)
	}
}
gen ret_w = .
forvalues i = 1/5{
	forvalues j = 0/1{
		replace ret_w = temp_ret_`i'_`j' if q_composite == `i' & buy == `j'
	}
}

* # stocks
egen num_stocks = count(permno), by(q_composite buy date)

keep date year month q_composite buy ret_e ret_w num_stocks
duplicates drop date q_composite buy, force

* Bring factors
merge m:1 year month using "${root}\Data\risk_factors"
drop if _merge == 2
drop _merge

* Excess returns
gen exret_e = ret_e - rf
gen exret_w = ret_w - rf

* Sqrt(n)
gen sqrt_n = sqrt(num_stocks)

save "${root}\Data\temp", replace

* Tables
use "${root}\Data\temp", clear

tabstat num_stocks, by(q)

* EW

keep if buy == 0 | buy == 1
bysort q_composite date (buy): gen exret_ew = ret_e - ret_e[_n-1]
keep if buy == 1

reg exret_ew
outreg2 using "${root}\Output\reg.xls", replace ctitle(Alpha) stats(coef tstat) bdec(2) tdec(2)

forvalues i = 1/5{

	reg exret_ew mktrf smb hml umd if q == `i', robust
	outreg2 using "${root}\Output\reg.xls", append ctitle(Alpha) stats(coef tstat) bdec(4) tdec(2)

}

keep if q == 1 | q == 5
bysort date (q): gen exret = exret_ew - exret_ew[_n-1]
keep if q == 5
reg exret mktrf smb hml umd, robust
outreg2 using "${root}\Output\reg.xls", append ctitle(Alpha) stats(coef tstat) bdec(4) tdec(2)


* VW
use "${root}\Data\temp", clear

keep if buy == 0 | buy == 1
bysort q_composite date (buy): gen exret_vw = ret_w - ret_w[_n-1]
keep if buy == 1


forvalues i = 1/5{

	reg exret_vw mktrf smb hml umd if q == `i', robust
	outreg2 using "${root}\Output\reg.xls", append ctitle(Alpha) stats(coef tstat) bdec(4) tdec(2)

}

keep if q == 1 | q == 5
bysort date (q): gen exret = exret_vw - exret_vw[_n-1]
keep if q == 5
reg exret mktrf smb hml umd, robust
outreg2 using "${root}\Output\reg.xls", append ctitle(Alpha) stats(coef tstat) bdec(4) tdec(2)


capture erase "${root}\Data\temp.dta"
